/*
 * Copyright (C) 2013 University of Washington
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
 * use this file except in compliance with the License. You may obtain a copy of
 * the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
 * License for the specific language governing permissions and limitations under
 * the License.
 */
package org.opendatakit.sensors.wifi;

import java.util.concurrent.atomic.AtomicBoolean;

import org.opendatakit.sensors.ODKSensor;
import org.opendatakit.sensors.manager.DiscoverableDevice;
import org.opendatakit.sensors.manager.DiscoverableDeviceState;
import org.opendatakit.sensors.manager.ODKSensorManager;

import android.bluetooth.BluetoothDevice;

/**
 * 
 * @author wbrunette@gmail.com
 * @author rohitchaudhri@gmail.com
 * 
 */
public class WifiDiscoverableDevice implements DiscoverableDevice {
	
	private WifiDevice wifiDevice;
	
	private ODKSensorManager sensorMgr;
	
	private AtomicBoolean connectionLost;
	
	private DiscoverableDeviceState dds;
	
	public WifiDiscoverableDevice(WifiDevice wfd, ODKSensorManager sensorManager) {
		this.wifiDevice=wfd;
		this.sensorMgr = sensorManager;		
		this.connectionLost = new AtomicBoolean(false); 
		this.dds=DiscoverableDeviceState.PAIRED;
	}
	
	public synchronized void updateWifiDeviceObject(WifiDevice updatedWifiDevice) {
		this.wifiDevice = updatedWifiDevice;
	}
	/*
	@Override
	public synchronized String getDeviceId() {
		return this.deviceID;
		//return btDevice.getAddress();
	}*/
	public synchronized void setDeviceState(DiscoverableDeviceState dds){
		this.dds=dds;
	}
	
	@Override
	public synchronized DiscoverableDeviceState getDeviceState() {
		/*if(connectionLost.get())
			return DiscoverableDeviceState.OUT_OF_RANGE;
		
		boolean bonded = (btDevice.getBondState() == BluetoothDevice.BOND_BONDED);
		if(!bonded)
			return DiscoverableDeviceState.UNPAIRED;
		
		ODKSensor sensor = sensorMgr.getSensor(getDeviceId());
		if(sensor != null)
			return DiscoverableDeviceState.REGISTERED;
		*/
		//TO BE IMPLEMENTED YET
		return dds;
	}
	
	@Override
	public void connectionLost() {
		connectionLost.set(true);
	}
	
	@Override
	public void connectionRestored() {
		connectionLost.set(false);
	}

	@Override
	public String getDeviceId() {
		return wifiDevice.getAddress();
	}

	@Override
	public String getDeviceName() {
		// TODO Auto-generated method stub
		return wifiDevice.getName();
	}

	
	
}
